package com.ct.application.sysrmenus.service.impl;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.sysrmenus.model.vo.SysMenusVO;
import com.ct.application.sysrmenus.request.SysMenusBatchRequest;
import com.ct.application.sysrmenus.request.SysMenusSaveRequest;
import com.ct.application.sysrmenus.request.SysMenusSearchRequest;
import com.ct.application.sysrmenus.request.SysMenusUpdateRequest;
import com.ct.application.sysrmenus.service.ISysMenusService;
import com.ct.rbac.enums.RbacResultEnum;
import com.ct.rbac.model.SysMenusModel;
import com.ct.rbac.orm.SysMenusDO;
import com.ct.rbac.repository.ISysMenusRespontory;
import com.ct.rbac.repository.ISysRoleMenusRespontory;
import com.ct.repository.convert.CopyConvertUtils;
import com.ct.utils.exception.ApiException;
import com.ct.utils.result.StatusEnums;
import com.ct.utils.valid.ValidatorUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysMenusServiceImpl implements ISysMenusService {


    @Resource
    private ISysMenusRespontory sysMenusRespontory;
    @Resource
    private ISysRoleMenusRespontory sysRoleMenusRespontory;


    /**
     * 分页查询菜单列表--租户使用
     * @param roleId
     * @return
     */
    @Override
    public List<SysMenusVO> findSystemPageMenu(Long roleId){
        List<SysMenusModel> respontoryRoles = sysMenusRespontory.findSystemPageMenu(roleId);
        return tranferListBo(respontoryRoles, SysMenusVO.class);
    }

    @Override
    public List<SysMenusVO> findPageMenu(SysMenusSearchRequest sysMenusSearchRequest) {
        List<SysMenusModel> respontoryRoles = sysMenusRespontory.findPageMenu( sysMenusSearchRequest.getStatus(), sysMenusSearchRequest.getKeyword());
        return tranferListBo(respontoryRoles, SysMenusVO.class);
    }

    @Override
    public SysMenusVO selectById(Long id) {
        SysMenusDO sysMenusDO = sysMenusRespontory.getById(id);
        return tranferBo(sysMenusDO, SysMenusVO.class);
    }

    @Override
    public Boolean deleteById(Long id) {
        // 如果角色被用户绑定了。直接返回
        if(sysRoleMenusRespontory.countMenuRole(id)){
            throw new ApiException(RbacResultEnum.MENU_YINYONG_API_EX);
        }
        return sysMenusRespontory.removeById(id);
    }

    @Override
    public SysMenusVO saveSysMenus(SysMenusSaveRequest sysMenusSaveRequest) {
        SysMenusDO sysMenusDO = CopyConvertUtils.copyProperties(sysMenusSaveRequest, SysMenusDO.class);
        boolean b = sysMenusRespontory.save(sysMenusDO);
        if (b){
            return CopyConvertUtils.copyProperties(sysMenusDO, SysMenusVO.class);
        }
        return null;
    }

    @Override
    public SysMenusVO updateSysMenus(SysMenusUpdateRequest sysMenusUpdateRequest) {
        SysMenusDO sysMenusDO = CopyConvertUtils.copyProperties(sysMenusUpdateRequest, SysMenusDO.class);
        boolean b = sysMenusRespontory.updateById(sysMenusDO);
        if (b){
            return CopyConvertUtils.copyProperties(sysMenusDO, SysMenusVO.class);
        }
        return null;
    }

    @Override
    public Boolean updateStatusSysMenus(StatusRequest statusRequest) {
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return sysMenusRespontory.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return sysMenusRespontory.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    @Override
    public Boolean batchStatus(SysMenusBatchRequest sysMenusBatchRequest) {
        List<Long> userIds = sysMenusBatchRequest.getIds();
        List<SysMenusDO> sysMenusDOS = userIds.stream().map(userId -> {
            SysMenusDO sysMenusDO = new SysMenusDO();
            sysMenusDO.setId(userId);
            sysMenusDO.setHidden(StatusEnums.STATUS.getCode());
            return sysMenusDO;
        }).collect(Collectors.toList());
        return sysMenusRespontory.updateBatchById(sysMenusDOS);
    }

    @Override
    public Boolean batchForbiddens(SysMenusBatchRequest sysMenusBatchRequest) {
        List<Long> userIds = sysMenusBatchRequest.getIds();
        List<SysMenusDO> sysMenusDOS = userIds.stream().map(userId -> {
            SysMenusDO sysMenusDO = new SysMenusDO();
            sysMenusDO.setId(userId);
            sysMenusDO.setHidden(StatusEnums.UN_STATUS.getCode());
            return sysMenusDO;
        }).collect(Collectors.toList());
        return sysMenusRespontory.updateBatchById(sysMenusDOS);
    }
}
